﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace DotNet.Data
{
    public class jobDal
    {
        IDBHelp db = DBHelp.Create();
        Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
        StringBuilder strSql = new StringBuilder();
        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int jobId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from job where jobId=@jobId ");
            htSyn.Add("jobId", jobId);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 获得招聘信息列表
        /// <summary>
        /// 获得招聘信息列表
        /// </summary>
        /// <returns></returns>
        public IList<job> GetModel()
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select jobId,Comany,jobClass,jobNeed,jobNum,jobBuMen,jobSex,jobTimes,jobStu,jobProfession,Term,jobRegion,jobInfo,jobPeople,jobTel,jobFax,jobEmail,jobAddress,jobDate from job ");
            return getProductsBySql(strSql.ToString());
        }
        #endregion

        #region 根据招聘信息号查询实体
        /// <summary>
        /// 根据招聘信息号查询实体
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public job GetModel(int jobId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select jobId,Comany,jobClass,jobNeed,jobNum,jobBuMen,jobSex,jobTimes,jobStu,jobProfession,Term,jobRegion,jobInfo,jobPeople,jobTel,jobFax,jobEmail,jobAddress,jobDate from job ");
            strSql.Append(" where jobId=@jobId ");
            htSyn.Add("@jobId", jobId);
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            job job = new job();
            if (dt.Rows.Count>0)
            {
                job.jobId = Convert.ToInt32(dt.Rows[0]["jobId"]);
                job.Comany = dt.Rows[0]["Comany"].ToString();
                job.jobClass = dt.Rows[0]["jobClass"].ToString();
                job.jobNeed = dt.Rows[0]["jobNeed"].ToString();
                job.jobNum = dt.Rows[0]["jobNum"].ToString();
                job.jobBuMen = dt.Rows[0]["jobBuMen"].ToString();
                job.jobSex = dt.Rows[0]["jobSex"].ToString();
                job.jobTimes = dt.Rows[0]["jobTimes"].ToString();
                job.jobStu = dt.Rows[0]["jobStu"].ToString();
                job.jobProfession = dt.Rows[0]["jobProfession"].ToString();
                job.Term = Convert.ToDateTime(dt.Rows[0]["Term"]);
                job.jobRegion = dt.Rows[0]["jobRegion"].ToString();
                job.jobInfo = dt.Rows[0]["jobInfo"].ToString();
                job.jobPeople = dt.Rows[0]["jobPeople"].ToString();
                job.jobTel = dt.Rows[0]["jobTel"].ToString();
                job.jobFax = dt.Rows[0]["jobFax"].ToString();
                job.jobEmail = dt.Rows[0]["jobEmail"].ToString();
                job.jobAddress = dt.Rows[0]["jobAddress"].ToString();
                job.jobDate = Convert.ToDateTime(dt.Rows[0]["jobDate"]);
            }           
            return job;
        }
        #endregion

        #region 修改招聘信息信息
        /// <summary>
        /// 修改招聘信息信息
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        public bool Update(job job)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("update job set ");
            strSql.Append("Comany=@Comany,");
            strSql.Append("jobClass=@jobClass,");
            strSql.Append("jobNeed=@jobNeed,");
            strSql.Append("jobNum=@jobNum,");
            strSql.Append("jobBuMen=@jobBuMen,");
            strSql.Append("jobSex=@jobSex,");
            strSql.Append("jobTimes=@jobTimes,");
            strSql.Append("jobStu=@jobStu,");
            strSql.Append("jobProfession=@jobProfession,");
            strSql.Append("Term=@Term,");
            strSql.Append("jobRegion=@jobRegion,");
            strSql.Append("jobInfo=@jobInfo,");
            strSql.Append("jobPeople=@jobPeople,");
            strSql.Append("jobTel=@jobTel,");
            strSql.Append("jobFax=@jobFax,");
            strSql.Append("jobEmail=@jobEmail,");
            strSql.Append("jobAddress=@jobAddress,");
            strSql.Append("jobDate=@jobDate");
            strSql.Append(" where jobId=@jobId ");
            htSyn.Add("@jobId", job.jobId);
            htSyn.Add("@Comany", job.Comany);
            htSyn.Add("@jobClass", job.jobClass);
            htSyn.Add("@jobNeed", job.jobNeed);
            htSyn.Add("@jobNum", job.jobNum);
            htSyn.Add("@jobBuMen", job.jobBuMen);
            htSyn.Add("@jobSex", job.jobSex);
            htSyn.Add("@jobTimes", job.jobTimes);
            htSyn.Add("@jobStu", job.jobStu);
            htSyn.Add("@jobProfession", job.jobProfession);
            htSyn.Add("@Term", job.Term);
            htSyn.Add("@jobRegion", job.jobRegion);
            htSyn.Add("@jobInfo", job.jobInfo);
            htSyn.Add("@jobPeople", job.jobPeople);
            htSyn.Add("@jobTel", job.jobTel);
            htSyn.Add("@jobFax", job.jobFax);
            htSyn.Add("@jobEmail", job.jobEmail);
            htSyn.Add("@jobAddress", job.jobAddress);
            htSyn.Add("@jobDate", job.jobDate);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 删除招聘信息根据招聘信息号
        /// <summary>
        /// 删除招聘信息根据招聘信息号
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public bool Delete(int jobId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from job where jobId=@jobId");
            htSyn.Add("jobId", jobId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 添加招聘信息
        /// <summary>
        /// 添加招聘信息
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        public bool Insert(job job)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("insert into job([Comany],[jobClass],[jobNeed],[jobNum],[jobBuMen],[jobSex],[jobTimes],[jobStu],[jobProfession],[Term],[jobRegion],[jobInfo],[jobPeople],[jobTel],[jobFax],[jobEmail],[jobAddress],[jobDate]) values (@Comany,@jobClass,@jobNeed,@jobNum,@jobBuMen,@jobSex,@jobTimes,@jobStu,@jobProfession,@Term,@jobRegion,@jobInfo,@jobPeople,@jobTel,@jobFax,@jobEmail,@jobAddress,@jobDate)");
            htSyn.Add("@Comany", job.Comany);
            htSyn.Add("@jobClass", job.jobClass);
            htSyn.Add("@jobNeed", job.jobNeed);
            htSyn.Add("@jobNum", job.jobNum);
            htSyn.Add("@jobBuMen", job.jobBuMen);
            htSyn.Add("@jobSex", job.jobSex);
            htSyn.Add("@jobTimes", job.jobTimes);
            htSyn.Add("@jobStu", job.jobStu);
            htSyn.Add("@jobProfession", job.jobProfession);
            htSyn.Add("@Term", job.Term);
            htSyn.Add("@jobRegion", job.jobRegion);
            htSyn.Add("@jobInfo", job.jobInfo);
            htSyn.Add("@jobPeople", job.jobPeople);
            htSyn.Add("@jobTel", job.jobTel);
            htSyn.Add("@jobFax", job.jobFax);
            htSyn.Add("@jobEmail", job.jobEmail);
            htSyn.Add("@jobAddress", job.jobAddress);
            htSyn.Add("@jobDate", job.jobDate);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        public IList<job> getProductsBySql(string strSql)
        {
            IList<job> list = new List<job>();
            DataTable dt = db.GetDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    job job = new job();
                    job.jobId = Convert.ToInt32(dr["jobId"]);
                    job.Comany = dr["Comany"].ToString();
                    job.jobClass = dr["jobClass"].ToString();
                    job.jobNeed = dr["jobNeed"].ToString();
                    job.jobNum = dr["jobNum"].ToString();
                    job.jobBuMen = dr["jobBuMen"].ToString();
                    job.jobSex = dr["jobSex"].ToString();
                    job.jobTimes = dr["jobTimes"].ToString();
                    job.jobStu = dr["jobStu"].ToString();
                    job.jobProfession = dr["jobProfession"].ToString();
                    job.Term = Convert.ToDateTime(dr["Term"]);
                    job.jobRegion = dr["jobRegion"].ToString();
                    job.jobInfo = dr["jobInfo"].ToString();
                    job.jobPeople = dr["jobPeople"].ToString();
                    job.jobTel = dr["jobTel"].ToString();
                    job.jobFax = dr["jobFax"].ToString();
                    job.jobEmail = dr["jobEmail"].ToString();
                    job.jobAddress = dr["jobAddress"].ToString();
                    job.jobDate = Convert.ToDateTime(dr["jobDate"]);
                    list.Add(job);
                }
            }
            return list;
        }
        #endregion

        #region 招聘分页
        /// <summary>
        /// 招聘分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public DataTable pageingForJob(int pageIndex,int pageSize) 
        {
            return db.GetMPageDataTable(pageIndex, pageSize, "jobId,Comany,jobClass,jobNeed,jobNum,jobBuMen,jobSex,jobTimes,jobStu,jobProfession,Term,jobRegion,jobInfo,jobPeople,jobTel,jobFax,jobEmail,jobAddress,jobDate", "job", "", "jobDate", true);
        }
        #endregion

        #region 获得招聘数量
        /// <summary>
        /// 获得招聘数量
        /// </summary>
        /// <returns></returns>
        public int getRecordCount() 
        {
            strSql.Remove(0, strSql.Length);
            object objRecord = null;
            objRecord = db.ExecuteScalar("select count(1) from job");
            int i=0;
            if (objRecord!=null)
            {
                i = Convert.ToInt32(objRecord);
            }
            return i;
        }
        #endregion
    }
}
